package com.copro.simple;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class SimpleUtils {

	public static void main(String[] arg) {
		String s = ",,";
		String[] aa = split(s);
		for (int i = 0; i < aa.length; i++)
			System.out.println("[" + i + "] " + aa[i]);
	}

	public static String[] split(String s) {
		return split(s, ',');
	}

	public static String[] split(String s, char seg) {
		if (null == s)
			return null;
		List<String> list = new LinkedList<String>();
		StringBuffer sb = new StringBuffer();
		char[] ca = s.toCharArray();
		for (int i = 0; i < ca.length; i++) {
			char c = ca[i];
			if (c == seg) {
				list.add(sb.toString());
				sb = new StringBuffer();
			} else {
				sb.append(c);
			}
		}
		if (sb.length() > 0)
			list.add(sb.toString());
		String[] ss = new String[list.size()];
		Iterator<String> it = list.iterator();
		int i = 0;
		while (it.hasNext()) {
			ss[i++] = it.next();
		}
		return ss;
	}
}
